﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;

public partial class Controls_Hotel_ctrHotelList : System.Web.UI.UserControl
{
    private int nCurrentLang = Support.GetCurrentLanguage();
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    private int nPageSize = 4;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initDataPaging();
        if (!IsPostBack)
        {
            this.setTitle();
            this.BindData(1);
        }
    }
    protected void setTitle()
    {
        int nHotel = ConvertData.ConvertToInt(Request.QueryString[Config.HOTELID]);
        lblWarning.Text = MessagesAlert.SEARCH_NO_ITEM[nDefaultLanguage];
       
            
        
    }
    private void BindData(int CurrentPageIndex)
    {
        try
        {
            Hotel objHotel = new Hotel();
            DataTable dtb = new DataTable();
            int nPageCount = 0;
            int nItemView = 0;
            int nHotel = ConvertData.ConvertToInt(Request.QueryString[Config.HOTELID]);
            if (nHotel > 0)
            {
                dtb = objHotel.Search(nItemView, "", 1, 0,0, 0, 1, nPageSize, ref nPageCount);
            }
            else
            {

                string sSearch = ConvertData.ConvertToString(Request.QueryString[Config.KEYWORD]);
                int nCity = ConvertData.ConvertToInt(Request.QueryString[Config.CITY]);
                int nStarRating = ConvertData.ConvertToInt(Request.QueryString[Config.STARRATING]);
                int nHotelPop = ConvertData.ConvertToInt(Request.QueryString[Config.HOTEL_POPULAR]);
                dtb = objHotel.Search(nItemView, sSearch, 0, nCity, nHotelPop, nStarRating, 1, nPageSize, ref nPageCount);
            }

            rptHotel.DataSource = dtb;
            rptHotel.DataBind();
            Pager.ItemCount = nPageCount;

            VisiblePaging(nPageCount);
            if (dtb.Rows.Count > 0)
                tblWarning.Visible = false;
            else
                tblWarning.Visible = true;
        }
        catch
        {
            Response.Redirect("~/Default.aspx");
        }
    }
    protected void rptHotel_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            ImageButton btnHotelImage = (ImageButton)e.Item.FindControl("btnHotelImage");
            HyperLink lnkHotelTitle = (HyperLink)e.Item.FindControl("lnkHotelTitle");
         
            Label lblValPriceFrom = (Label)e.Item.FindControl("lblValPriceFrom");
            Label lblValPriceFromTo = (Label)e.Item.FindControl("lblValPriceFromTo");
            //Label lblValCountry = (Label)e.Item.FindControl("lblValCountry");
            //Label lblCity = (Label)e.Item.FindControl("lblCity");
            //Label lblCountry = (Label)e.Item.FindControl("lblCountry");
            //Label lblValRegion = (Label)e.Item.FindControl("lblValRegion");
            //Label lblValCity = (Label)e.Item.FindControl("lblValCity");
            Label lblValAddress = (Label)e.Item.FindControl("lblValAddress");
            HtmlImage lblValStarRating = (HtmlImage)e.Item.FindControl("lblValStarRating");

            Support.GetStar(lblValStarRating, ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "StarRating")));
            lblValPriceFrom.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title1"));
            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "HotelID"));
            int nCity = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "CityID"));
            City objCity = new City();
            Region objRegion = new Region();
            Country objCountry = new Country();
            if (nCity > 0)
            {
                objCity.LoadById(nCity);
                objRegion.LoadById(objCity.Data.RegionID);
                objCountry.LoadById(objRegion.Data.CountryID);
            }

            lnkHotelTitle.NavigateUrl = Config.ROOT + Pages.FrontEnds.HOTEL_DETAIL + "?" + Config.HOTELID + "=" + nID;
            switch (nCurrentLang)
            {
                case 1:
                    if (nCity > 0)
                    {
                        //lblValRegion.Text = objRegion.Data.RegionName1;
                        //lblValCity.Text = objCity.Data.CityName1;
                        //lblValCountry.Text = objCountry.Data.CountryName1;
                    }
                  
                    lnkHotelTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title1"));
                    lblValAddress.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Address1"));
                    break;
                case 2:
                    if (nCity > 0)
                    {
                        //lblValRegion.Text = objRegion.Data.RegionName2;
                        //lblValCity.Text = objCity.Data.CityName2;
                        //lblValCountry.Text = objCountry.Data.CountryName2;
                    }
                    lnkHotelTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title2"));
                    lblValAddress.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Address2"));
                      
                    break;
                case 3:
                    if (nCity > 0)
                    {
                        //lblValRegion.Text = objRegion.Data.RegionName3;
                        //lblValCity.Text = objCity.Data.CityName3;
                        //lblValCountry.Text = objCountry.Data.CountryName3;
                    }
                        
                    lnkHotelTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title3"));
                    lblValAddress.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Address3"));
                    break;
            }
            btnHotelImage.ImageUrl = Config.ROOT + Config.IMAGE_HOTEL_DEFAULT_UPLOAD + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Image"));
            btnHotelImage.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "HotelID"));
            lblValPriceFrom.Text = "$" + this.GetPriceFrom(nID);
            lblValPriceFromTo.Text = this.GetPriceto(nID);
        }
    }

    private string GetPriceFrom(int nID)
    {
        Hotel obj = new Hotel();
        obj.LoadById(nID);
        string sPriceFrom = "";
        switch (nCurrentLang)
        { 
            case 1:
                sPriceFrom = ConvertData.ConvertToString(obj.Data.PriceFrom1);
                break;
            case 2:
                sPriceFrom = ConvertData.ConvertToString(obj.Data.PriceFrom2);
                break;
            case 3:
                sPriceFrom = ConvertData.ConvertToString(obj.Data.PriceFrom3);
                break;
        }
        return sPriceFrom;
    }

    private string GetPriceto(int nID)
    {
        Hotel obj = new Hotel();
        obj.LoadById(nID);
        string sPriceFrom = "";
        switch (nCurrentLang)
        {
            case 1:
                sPriceFrom = ConvertData.ConvertToString(obj.Data.PriceFromTo1);
                break;
            case 2:
                sPriceFrom = ConvertData.ConvertToString(obj.Data.PriceFromTo2);
                break;
            case 3:
                sPriceFrom = ConvertData.ConvertToString(obj.Data.PriceFromTo3);
                break;
        }
        return sPriceFrom;
    }
    protected void rptHotel_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);

        switch (strCommand)
        {
            case "image":
                string sDetail = Config.ROOT + Pages.FrontEnds.HOTEL_DETAIL + "?" + Config.HOTELID + "=" + nID;
                Response.Redirect(sDetail);
                break;
        }
    }
    public string GetCurrentPageName()
    {
        string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
        string sRet = oInfo.Name;
        return sRet;
    }
    private void initDataPaging()
    {
        Pager.PageSize = nPageSize;
    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager.CurrentIndex = currnetPageIndx;
        this.BindData(currnetPageIndx);
    }
    private void VisiblePaging(int intPagecount)
    {
        if (intPagecount > nPageSize)
            Pager.Visible = true;
        else
            Pager.Visible = false;
    }
}
